package com.lhoverc.utils;

import com.lhoverc.pojo.dto.SendInvitationCodeDto;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: LiCheng
 * @CreateTime: 2025-04-27
 * @Description:
 * @Version: 1.0
 */
@Component
public class MailMsg {

    @Resource
    private JavaMailSenderImpl mailSender;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public boolean sendReCaptcha(String email) throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //生成随机验证码
        String code = CodeGeneratorUtil.generateCode(6);
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //设置一个html邮件信息
        String localDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String htmlContent = "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>邮箱验证码</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <p>尊敬的用户，您好！</p>\n" +
                "    <p>您正在使用 [智能社区管理系统] 账号，本次验证码为：<span style=\"font-size: 24px; font-weight: bold;\">" + code + "</span>，验证码有效期为 5 分钟。</p>\n" +
                "    <p>请妥善保管验证码，切勿将其泄露给他人。如果这不是您本人的操作，请忽略此邮件。</p>\n" +
                "    <p>[应用名称] 智能社区管理系统</p>\n" +
                "    <p>[具体日期] " + localDateTime + "</p>\n" +
                "</body>\n" +
                "</html>";
        helper.setText(htmlContent, true);
        //设置邮件主题名
        helper.setSubject("智能社区管理系统----验证码");
        //发给谁-》邮箱地址
        helper.setTo(email);
        //谁发的-》发送人邮箱
        helper.setFrom("2138531725@qq.com");
        //将邮箱验证码以邮件地址为key存入redis,1分钟过期
        redisTemplate.opsForValue().set(email, code, Duration.ofMinutes(5));
        mailSender.send(mimeMessage);
        return true;
    }

    public String getValue(String email) {
        return redisTemplate.opsForValue().get(email);
    }


    public String sendInvitationCode(SendInvitationCodeDto sendInvitationCodeDto) throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //生成随机验证码
        String code = CodeGeneratorUtil.generateCode(6);
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //设置一个html邮件信息
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化到达时间和离开时间
        String arriveTime = sendInvitationCodeDto.getArriveTime().format(formatter);
        String leaveTime = sendInvitationCodeDto.getLeaveTime().format(formatter);
        String htmlContent = "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>智能社区管理系统邀请</title>\n" +
                "    <style>\n" +
                "        body {\n" +
                "            font-family: 'Inter', sans-serif;\n" +
                "            background-color: #f4f4f9;\n" +
                "            margin: 0;\n" +
                "            padding: 0;\n" +
                "        }\n" +
                "        .container {\n" +
                "            max-width: 600px;\n" +
                "            margin: 20px auto;\n" +
                "            background-color: #ffffff;\n" +
                "            border-radius: 8px;\n" +
                "            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);\n" +
                "        }\n" +
                "        .header {\n" +
                "            background-color: #28a745;\n" +
                "            color: #ffffff;\n" +
                "            text-align: center;\n" +
                "            padding: 20px;\n" +
                "            border-top-left-radius: 8px;\n" +
                "            border-top-right-radius: 8px;\n" +
                "        }\n" +
                "        .content {\n" +
                "            padding: 20px;\n" +
                "        }\n" +
                "        .invitation-code {\n" +
                "            font-size: 24px;\n" +
                "            font-weight: bold;\n" +
                "            color: #28a745;\n" +
                "            text-align: center;\n" +
                "            margin: 20px 0;\n" +
                "            background-color: #e9f7ef;\n" +
                "            padding: 10px;\n" +
                "            border-radius: 4px;\n" +
                "        }\n" +
                "        .button {\n" +
                "            display: block;\n" +
                "            width: 200px;\n" +
                "            margin: 20px auto;\n" +
                "            background-color: #28a745;\n" +
                "            color: #ffffff;\n" +
                "            text-align: center;\n" +
                "            padding: 10px;\n" +
                "            text-decoration: none;\n" +
                "            border-radius: 4px;\n" +
                "            transition: background-color 0.3s ease;\n" +
                "        }\n" +
                "        .button:hover {\n" +
                "            background-color: #218838;\n" +
                "        }\n" +
                "        .footer {\n" +
                "            text-align: center;\n" +
                "            padding: 20px;\n" +
                "            color: #888888;\n" +
                "            font-size: 12px;\n" +
                "        }\n" +
                "        table {\n" +
                "            width: 100%;\n" +
                "            border-collapse: collapse;\n" +
                "            margin-top: 20px;\n" +
                "        }\n" +
                "        th, td {\n" +
                "            border: 1px solid #ddd;\n" +
                "            padding: 8px;\n" +
                "            text-align: left;\n" +
                "        }\n" +
                "        th {\n" +
                "            background-color: #f2f2f2;\n" +
                "        }\n" +
                "        table tr {\n" +
                "            display: block;\n" +
                "        }\n" +
                "        table th,\n" +
                "        table td {\n" +
                "            display: block;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <div class=\"header\">\n" +
                "            <h1>诚邀您进入lhoverc智能社区</h1>\n" +
                "        </div>\n" +
                "        <div class=\"content\">\n" +
                "            <p>尊敬的访客，您好！</p>\n" +
                "            <p>为了方便您正常出入小区，我们为您提供了专属的邀请码。以下是您的相关信息：</p>\n" +
                "            <table>\n" +
                "                <tr>\n" +
                "                    <th>业主名</th>\n" +
                "                    <td>" + sendInvitationCodeDto.getUsername() + "</td>\n" +
                "                </tr>\n" +
                "                <tr>\n" +
                "                    <th>目的地</th>\n" +
                "                    <td>" + sendInvitationCodeDto.getDestination() + "</td>\n" +
                "                </tr>\n" +
                "                <tr>\n" +
                "                    <th>访客姓名</th>\n" +
                "                    <td>" + sendInvitationCodeDto.getVisitorName() + "</td>\n" +
                "                </tr>\n" +
                "                <tr>\n" +
                "                    <th>访客照片</th>\n" +
                "                    <td><img src=\"" + sendInvitationCodeDto.getVisitorImage() + "\"/></td>\n" +
                "                </tr>\n" +
                "                <tr>\n" +
                "                    <th>事由</th>\n" +
                "                    <td>" + sendInvitationCodeDto.getToDo() + "</td>\n" +
                "                </tr>\n" +
                "                <tr>\n" +
                "                    <th>时间</th>\n" +
                "                    <td>" + arriveTime + "到" + leaveTime + "</td>\n" +
                "                </tr>\n" +
                "            </table>\n" +
                "                <p>您正在使用 [智能社区管理系统] ，本次邀请码为：<div style=\"font-size: 24px; font-weight: bold;\">" + code + "</div>在规定时间内使用。</p>\n" +
                "    <p>请妥善保管邀请码，切勿将其泄露给他人。如果您本人不知情或者信息不符，请忽略此邮件。</p>\n" +
                "        </div>\n" +
                "        <div class=\"footer\">\n" +
                "            <p>&copy; 2025 lhoverc 智能社区管理系统。保留所有权利。</p>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";
        helper.setText(htmlContent, true);
        //设置邮件主题名
        helper.setSubject("智能社区管理系统----验证码");
        //发给谁-》邮箱地址
        helper.setTo(sendInvitationCodeDto.getVisitorEmail());
        //谁发的-》发送人邮箱
        helper.setFrom("2138531725@qq.com");
        mailSender.send(mimeMessage);
        return code;
    }

}
